package com.sam.chat.service.impl;

import com.sam.chat.entity.ChatFriendDao;
import com.sam.chat.mapper.ChatFriendMapper;
import com.sam.chat.mapper.ChatUserMapper;
import com.sam.chat.service.IChatFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sam.chat.utils.DateUtils;
import com.sam.chat.vo.FriendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sam
 * @since 2023-01-07
 */
@Service
public class ChatFriendServiceImpl extends ServiceImpl<ChatFriendMapper, ChatFriendDao> implements IChatFriendService {
    @Autowired
    private ChatFriendMapper chatFriendMapper;
    @Autowired
    private ChatUserMapper chatUserMapper;

    @Override
    public List<FriendVo> getFriendsList(String uid) {
        List<FriendVo> friendVoList = chatFriendMapper.getFriendsList(uid);
        return friendVoList;
    }

    @Override
    public FriendVo getFriendInfo(String uid, String friendId) {
        FriendVo friendVo = chatFriendMapper.getFriendInfo(uid, friendId);
        return friendVo;
    }

    @Override
    public boolean addFriend1(String userId, String friendUsername) {
        Integer friendId = chatUserMapper.getUserId(friendUsername);
        ChatFriendDao chatFriendDao = new ChatFriendDao();
        chatFriendDao.setFriendId(friendId);
        chatFriendDao.setUid(Integer.parseInt(userId));
        chatFriendDao.setCreateTime(LocalDateTime.now());
        chatFriendDao.setUpdateTime(LocalDateTime.now());
        int result = chatFriendMapper.insert(chatFriendDao);

        ChatFriendDao chatFriendDao2 = new ChatFriendDao();
        chatFriendDao2.setUid(friendId);
        chatFriendDao2.setFriendId(Integer.parseInt(userId));
        chatFriendDao2.setCreateTime(LocalDateTime.now());
        chatFriendDao2.setUpdateTime(LocalDateTime.now());
        int result2 = chatFriendMapper.insert(chatFriendDao2);
        return result == 1&&result2 ==1;
    }

    @Override
    public boolean addFriend2(String userId, String friendId) {
        //加好友，双方都要加
        ChatFriendDao chatFriendDao = new ChatFriendDao();
        chatFriendDao.setFriendId(Integer.parseInt(friendId));
        chatFriendDao.setUid(Integer.parseInt(userId));
        chatFriendDao.setCreateTime(LocalDateTime.now());
        chatFriendDao.setUpdateTime(LocalDateTime.now());
        int result1 = chatFriendMapper.insert(chatFriendDao);

        ChatFriendDao chatFriendDao2 = new ChatFriendDao();
        chatFriendDao2.setUid(Integer.parseInt(friendId));
        chatFriendDao2.setFriendId(Integer.parseInt(userId));
        chatFriendDao2.setCreateTime(LocalDateTime.now());
        chatFriendDao2.setUpdateTime(LocalDateTime.now());
        int result2 = chatFriendMapper.insert(chatFriendDao2);
        return result1 == 1 && result2 ==1;
    }

    @Override
    public boolean checkFriendId(String uid ,String friendId) {
        int count = chatFriendMapper.checkFriendId(uid,friendId);
        return count == 0;
    }

    @Override
    public boolean deleteFriend(String userId, String friendId) {
        int result = chatFriendMapper.deleteFriend(userId,friendId);
        return false;
    }
}
